const toolMethods = {

	getRandomInt(min, max) {
		min = Math.ceil(min);
		max = Math.floor(max);
		return Math.floor(Math.random() * (max - min + 1)) + min;
	},
	/**
	 * @description JS对象深度合并
	 * @param {object} target 需要拷贝的对象
	 * @param {object} source 拷贝的来源对象
	 * @returns {object|boolean} 深度合并后的对象或者false（入参有不是对象）
	 */
	deepMerge(target = {}, source = {}) {
		// 深拷贝目标对象，防止影响原始数据
		target = toolMethods.deepClone(target);

		if (typeof target !== 'object' || target === null || typeof source !== 'object' || source === null) {
			return target;
		}

		// 如果目标和源都是数组，处理合并数组逻辑
		const merged = Array.isArray(target) ? target.slice() : {
			...target
		};

		for (const prop in source) {
			if (!source.hasOwnProperty(prop)) continue;

			const sourceValue = source[prop];
			const targetValue = merged[prop];

			if (sourceValue instanceof Date) {
				merged[prop] = new Date(sourceValue);
			} else if (sourceValue instanceof RegExp) {
				merged[prop] = new RegExp(sourceValue);
			} else if (sourceValue instanceof Map) {
				merged[prop] = new Map(sourceValue);
			} else if (sourceValue instanceof Set) {
				merged[prop] = new Set(sourceValue);
			} else if (Array.isArray(sourceValue)) {
				// 当源是数组时递归合并数组
				merged[prop] = Array.isArray(targetValue) ?
					toolMethods.deepMerge(targetValue, sourceValue) :
					sourceValue.slice(); // 如果目标不是数组则直接拷贝源数组
			} else if (typeof sourceValue === 'object' && sourceValue !== null) {
				merged[prop] = toolMethods.deepMerge(targetValue, sourceValue);
			} else {
				merged[prop] = sourceValue;
			}
		}

		return merged;
	},
	/**
	 * @description 深度克隆
	 * @param {object} obj 需要深度克隆的对象
	 * @param cache 缓存
	 * @returns {*} 克隆后的对象或者原值（不是对象）
	 */
	deepClone(obj, cache = new WeakMap()) {
		if (obj === null || typeof obj !== 'object') return obj;
		if (cache.has(obj)) return cache.get(obj);
		let clone;
		if (obj instanceof Date) {
			clone = new Date(obj.getTime());
		} else if (obj instanceof RegExp) {
			clone = new RegExp(obj);
		} else if (obj instanceof Map) {
			clone = new Map(Array.from(obj, ([key, value]) => [key, toolMethods.deepClone(value, cache)]));
		} else if (obj instanceof Set) {
			clone = new Set(Array.from(obj, value => toolMethods.deepClone(value, cache)));
		} else if (Array.isArray(obj)) {
			clone = obj.map(value => toolMethods.deepClone(value, cache));
		} else if (Object.prototype.toString.call(obj) === '[object Object]') {
			clone = Object.create(Object.getPrototypeOf(obj));
			cache.set(obj, clone);
			for (const [key, value] of Object.entries(obj)) {
				clone[key] = toolMethods.deepClone(value, cache);
			}
		} else {
			clone = Object.assign({}, obj);
		}
		cache.set(obj, clone);
		return clone;
	},

	closeLoading() {
		// #ifndef APP-PLUS
		uni.hideLoading();
		// #endif
		// #ifdef APP-PLUS
		plus.nativeUI.closeWaiting();
		// #endif
	},
	jumpTo(url, type) {
		if (!url) {
			uni.navigateBack(1);
		} else {
			let tabBar = ['/pages/MyPet/MyPet', '/pages/outside/index', '/pages/flashSale/index',
				'/pages/book/index',
				'/pages/mine/index'
			];
			let iftabBar = tabBar.filter(item => item === url);
			let jumpType = type || 0;
			if (iftabBar && iftabBar.length > 0) {
				uni.switchTab({
					url: iftabBar[0]
				})
			} else {
				if (jumpType === 1) {
					redirectTo
					uni.redirectTo({
						url: url
					})
				} else if (jumpType === 2) {
					uni.reLaunch({
						url: url
					})
				} else {
					uni.navigateTo({
						url: url
					})
				}
			}
		}
	},
	showTips(msg, Fn, time = 1550) {
		// #ifdef APP
		plus.nativeUI.toast(msg, {

			style: "inline",
			align: "center"
		});
		// #endif
		// #ifndef APP
		uni.showToast({
			title: msg,
			icon: 'none',
			mask: true,
			duration: time
		});
		// #endif
		if (Fn && typeof Fn === 'function') {
			setTimeout(() => {
				Fn();
			}, time);
		}
	},
	//初始化对象赋值
	initRow(row, params) {
		if (params) {
			let rowKeys = Object.keys(row);
			let paramsKeys = Object.keys(params);
			let data = {};
			rowKeys.forEach((item, index) => {
				if (paramsKeys.indexOf(item) > -1) {
					data[item] = params[item];
				}
			});
			return data;
		} else {
			return toolMethods.deepCopy(row);
		}
	},
	showLoading(msg) {
		// #ifndef APP-PLUS
		uni.showLoading({
			mask: true,
			title: msg || ''
		})
		// #endif
		// #ifdef APP-PLUS
		plus.nativeUI.showWaiting(msg || 'Loding..', {
			// background: 'rgba(255,255,255,0)',
			background: '#fff',
			color: '#222',
			style: 'black',
			loading: {
				display: 'block',
				height: '18',
				icon: '/static/LOGO.png'
			}
		});
		// #endif
	},
	//格式化金额
	formatEnNumber(num) {
		if (num >= 1000000000) {
			return Math.floor((num / 1000000000) * 100) / 100 + 'B';
		} else if (num >= 1000000) {
			return Math.floor((num / 1000000) * 100) / 100 + 'M';
		} else if (num >= 1000) {
			return Math.floor((num / 1000) * 100) / 100 + 'K';
		} else {
			return Math.floor(num).toString();
		}
	}


};


export default toolMethods;